import api from '@api'
const state = {
  balance: 0,
  record: {
    list: [],
    pageNum: 0,
    page: 1
  }
}

const getters = {
  ['getBalance'] (state) {
    return state.balance
  },
  ['getRecord'] (state) {
    return state.record
  }
}

const actions = {
  async ['getBalance'] ({commit}) {
    let {data: {uMoney,StatusCode}} =  await api.get('api/v1/UserInfo/userMoney.ashx')
    if (StatusCode=='200') {
     commit('setBalance', uMoney)
    }
  },
  async ['getRecord'] ({commit}, page) {
    if(page ===1) {
      commit('clearRecodList');
    }
    let {data: {rechargeList,pageNum, StatusCode}} = await api.get('api/v1/UserInfo/rechargeList.ashx', {
      CurrentPage: page || 1,
      PageSize: 10,
      rechargeType: 1
    });
    if (StatusCode=='200') {
      commit('setRecodList', {list: rechargeList, pageNum: parseInt(pageNum), page })
    }
  }
}

const mutations = {
  ['setBalance'] (state, balance) {
    state.balance = balance
  },
  ['setRecodList'] (state, {list, pageNum, page}) {
    state.record.list = state.record.list.concat(list);
    state.record.pageNum = pageNum;
    state.record.page = page;
  },
  ['clearRecodList'] (state) {
    state.record={
      list: [],
      pageNum: 0,
      page: 1
    }
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}

